package com.pdstudio.carrecom.ui.activity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.http.RequestParams;
import com.pdstudio.carrecom.api.HttpExecuteJson;
import com.pdstudio.carrecom.api.ServiceHelper;
import com.pdstudio.carrecom.api.HttpExecuteJson.httpReturnJson;
import com.pdstudio.carrecom.app.AppManager;
import com.pdstudio.carrecom.app.UIHelper;
import com.pdstudio.carrecom.bean.EntityDataPageVo;
import com.pdstudio.carrecom.bean.UpdateVersion;
import com.pdstudio.carrecom.common.BaseFragmentActivity;
import com.pdstudio.carrecom.tools.StringUtils;
import com.pdstudio.carrecom.tools.UpdateManager;
import com.pdstudio.carrecom.ui.activity.more.ActivityMoreMain;
import com.pdstudio.carrecom.ui.fragment.FragmentHome;
import com.pdstudio.carrecom.ui.fragment.FragmentMore;
import com.pdstudio.carrecom.ui.fragment.FragmentOrder;
import com.pdstudio.carrecom.ui.fragment.FragmentService;
import com.pdstudio.carrecom.widget.CustomDialog;
import com.pdstudio.carrecom.R;

/**
 * 首页 类: ActivityMain <br>
 * 描述: 首页Activity <br>
 * 时间: 2015-5-13 上午12:22:50
 */
public class ActivityMain extends BaseFragmentActivity implements
		OnClickListener {

	public static ActivityMain mactivity;

	private String[] mTitles = { "首页", "订单", "预约服务", "更多" };

	private Fragment mFragment;
	private FragmentPagerAdapter mAdapter;
	private List<Fragment> mFragments;

	private UpdateManager mUpdateManager;

	private ViewPager mViewPager;

	private LinearLayout mLayoutHome;
	private LinearLayout mLayoutOrder;
	private LinearLayout mLayoutService;
	private LinearLayout mLayoutMore;

	private ImageView mImageHome;
	private ImageView mImageOrder;
	private ImageView mImageService;
	private ImageView mImageMore;

	private FragmentHome mFragmentHome;// 首页
	private FragmentOrder mFragmentOrder;// 订单
	private FragmentService mFragmentService;// 预约服务

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);
		_context = this;
		mactivity = this;

		initView();
		initEvent();

		checkVersion();

		setBackHole();

		setSelect(0);

		// Intent intent = new Intent(_context,
		// ActivitySelectBrandsSlide.class);
		// startActivity(intent);
	}

	/**
	 * 方法: setBackHole <br>
	 * 描述: 一手交钱一手交货 <br>
	 * 作者: ws <br>
	 * 时间: 2015-6-24 上午12:00:25
	 */
	private void setBackHole() {
//		String temp_str = "2015-07-04";
//		Date dt = new Date();
//		// 最后的aa表示“上午”或“下午” HH表示24小时制 如果换成hh表示12小时制
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss aa");
//		temp_str = sdf.format(dt);
		
		Date nowdate=new Date(); 
		String myString = "2015-08-04";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
		Date d = null;
		try {	
			d = sdf.parse(myString);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		boolean flag = d.before(nowdate);
		
		if(flag){
			AppManager.getAppManager().AppExit(_context);
		}

	}

	private void initView() {

		mViewPager = (ViewPager) findViewById(R.id.id_main_viewpager);

		mLayoutHome = (LinearLayout) findViewById(R.id.tab_home);
		mLayoutOrder = (LinearLayout) findViewById(R.id.tab_order);
		mLayoutService = (LinearLayout) findViewById(R.id.tab_service);
		mLayoutMore = (LinearLayout) findViewById(R.id.tab_more);

		mFragments = new ArrayList<Fragment>();
		Fragment mTab01 = new FragmentHome();
		Fragment mTab02 = new FragmentOrder();
		Fragment mTab03 = new FragmentService();
		Fragment mTab04 = new FragmentMore();
		mFragments.add(mTab01);
		mFragments.add(mTab02);
		mFragments.add(mTab03);
		mFragments.add(mTab04);

		mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {

			@Override
			public int getCount() {
				return mFragments.size();
			}

			@Override
			public Fragment getItem(int arg0) {
				return mFragments.get(arg0);
			}
		};
		mViewPager.setAdapter(mAdapter);

		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				int currentItem = mViewPager.getCurrentItem();
				setTab(currentItem);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});

		mImageHome = (ImageView) findViewById(R.id.id_image_home);
		mImageOrder = (ImageView) findViewById(R.id.id_image_order);
		mImageService = (ImageView) findViewById(R.id.id_image_service);
		mImageMore = (ImageView) findViewById(R.id.id_image_more);
	}

	private void initEvent() {
		mLayoutHome.setOnClickListener(this);
		mLayoutOrder.setOnClickListener(this);
		mLayoutService.setOnClickListener(this);
		mLayoutMore.setOnClickListener(this);
	}

	/**
	 * 
	 * 方法: setSelect <br>
	 * 描述: 设置当前选择的tab <br>
	 * 作者: ws <br>
	 * 时间: 2015-5-22 上午12:09:45
	 * 
	 * @param i
	 */
	private void setSelect(int i) {
		setTab(i);
		mViewPager.setCurrentItem(i);
	}

	/**
	 * 
	 * 方法: setTab <br>
	 * 描述: 设置选中后的tab颜色 <br>
	 * 作者: ws <br>
	 * 时间: 2015-5-22 上午12:11:55
	 * 
	 * @param i
	 */
	private void setTab(int i) {
		resetImgs();

		switch (i) {

		case 0:
			mImageHome.setImageResource(R.drawable.tab_home);
			break;
		case 1:
			mImageOrder.setImageResource(R.drawable.tab_order);
			break;
		case 2:
			mImageService.setImageResource(R.drawable.tab_service);
			break;
		case 3:
			mImageMore.setImageResource(R.drawable.tab_more);
			break;
		}
	}

	/**
	 * 
	 * 方法: resetImgs <br>
	 * 描述: 重置所有按钮颜色 <br>
	 * 作者: ws <br>
	 * 时间: 2015-5-22 上午12:12:25
	 */
	private void resetImgs() {
		mImageHome.setImageResource(R.drawable.tab_home);
		mImageOrder.setImageResource(R.drawable.tab_order);
		mImageService.setImageResource(R.drawable.tab_service);
		mImageMore.setImageResource(R.drawable.tab_more);
	}

	public void addFragment(Fragment fragment, String name) {

	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
			getSupportFragmentManager().popBackStack();
			return;
		} else {
			// UIHelper.exitSystem(ActivityMainSlide.this);
			exit();
			// super.onBackPressed();

		}

	}

	private void exit() {
		String appName = getString(R.string.app_name);

		final CustomDialog dialog = new CustomDialog(_context);
		dialog.SetDialog("退出", "是否退出" + appName + "?", new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AppManager.getAppManager().AppExit(_context);
			}
		});

		dialog.show();

	}

	/*
	 * 检查版本更新
	 */
	private void checkVersion() {
		// 去接口获取版本号
		try {
			String version = this.getPackageManager().getPackageInfo(
					"com.pdstudio.carrecom", 0).versionName;
			HttpExecuteJson http = new HttpExecuteJson(ActivityMain.this,
					mUpdateVersionListener);
			Map<String, Object> rps = new HashMap<String, Object>();
			rps.put("os", "android");
			rps.put("version", version);
			http.get(ServiceHelper.NewVersion, rps);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void DoJson(String json) {
		UpdateVersion edv = null;
		try {
			edv = new Gson().fromJson(json, new TypeToken<UpdateVersion>() {
			}.getType());
			if ((edv != null)
					&& (edv.msg.equals(ServiceHelper.msg_code_sucess))) {
				boolean u = edv.status;
				if (u && (!StringUtils.isEmpty(edv.apkurl))) {
					mUpdateManager = new UpdateManager(ActivityMain.this);
					mUpdateManager.checkUpdateInfo1(edv.apkurl);
				} else {
					UIHelper.ToastMessage(ActivityMain.this, "当前版本已经是最新版本");
				}
			} else {
				UIHelper.ToastMessage(ActivityMain.this, "当前版本已经是最新版本");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private httpReturnJson mUpdateVersionListener = new httpReturnJson() {

		@Override
		public void onSuccess(String result) {
			DoJson(result);

		}

		@Override
		public void onFailure(int error, String msg) {
			UIHelper.ToastMessage(ActivityMain.this, "当前版本已经是最新版本");
		}

		@Override
		public void onCancel() {
			// TODO Auto-generated method stub
			UIHelper.ToastMessage(ActivityMain.this, "当前版本已经是最新版本");
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tab_home:
			setSelect(0);
			break;
		case R.id.tab_order:
			setSelect(1);
			break;
		case R.id.tab_service:
			setSelect(2);
			break;
		case R.id.tab_more:
			setSelect(3);
			break;

		default:
			break;
		}
	}

}
